﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NomClasses;

namespace NomCafe
{
    public partial class frmItemManager : DevComponents.DotNetBar.Office2007RibbonForm
    {
        string idItem;
        public frmItemManager()
        {
            InitializeComponent();
            listViewItem.Columns[0].Width = 0;
        }

        private void frmItemManager_Load(object sender, EventArgs e)
        {
            getDataListViewItem();
            getDataCombobox();
        }
        
        private void getDataListViewItem()
        {
            listViewItem.Items.Clear();
            // Lấy item cho listViewItem

            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_item> Item = nc.nom_items;
            foreach (NomClasses.nom_item ite in Item)
            {
                string[] row = {ite.name,ite.nom_item_category.name , ite.price.ToString(),
                               ite.nom_specification.specification ,ite.description};
                listViewItem.Items.Add(ite.id.ToString()).SubItems.AddRange(row);
            }
        }

        // Lấy dữ liệu cho các cbb loại món và quy cách
        private void getDataCombobox()
        { 
            // Lấy dữ liệu cbb loại món
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_item_category> ItemCategory = nc.nom_item_categories;
            cbbTypeItem.DataSource = ItemCategory.ToArray();
            cbbTypeItem.DisplayMember = "name";
            cbbTypeItem.ValueMember = "id";
            
            // Lấy dữ liệu cbb quy cách            
            System.Data.Linq.Table<NomClasses.nom_specification> spec = nc.nom_specifications;
            cbbMode.DataSource = spec.ToArray();
            cbbMode.DisplayMember = "specification";
            cbbMode.ValueMember = "id";
            
        }

        private void btnRenew_Click(object sender, EventArgs e)
        {
            superValidator1.ClearFailedValidations();            
            getDataListViewItem();
            getDataCombobox();
            txtNameItem.Text = "";
            inputPrice.Text = null;            
            comboBoxEx3.Text = "";
            txtNote.Text = "";

            btnAccept.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (cbbTypeItem.Text == "" || txtNameItem.Text == "" || inputPrice.Text == null ||
                    cbbMode.Text == "")
                {
                    MessageBox.Show(languageManager.INSERT_ITEM_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    NomDataContext nc = new NomDataContext();
                    var itemList = from item in nc.nom_items
                                   select item;
                    foreach (var item in itemList)
                    {
                        if (txtNameItem.Text.Trim().ToLower().Equals(item.name.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_ITEM_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_item item = new nom_item();
                        item.name = txtNameItem.Text;
                        item.item_category_id = Convert.ToInt16(cbbTypeItem.SelectedValue);
                        item.price = inputPrice.Value;
                        item.specification_id = Convert.ToInt16(cbbMode.SelectedValue);
                        //item.formula = comboBoxEx3.Text;
                        //item.image = "";
                        item.description = txtNote.Text;
                        nc.nom_items.InsertOnSubmit(item);
                        nc.SubmitChanges();
                                                
                        getDataListViewItem();

                        txtNameItem.Text = "";
                        inputPrice.Text = null;                        
                        txtNote.Text = "";                                                
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void listViewItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnAccept.Enabled = false;

            idItem = "";
            for (int i = 0; i < listViewItem.Items.Count; i++)
            {
                if (listViewItem.Items[i].Selected)
                {
                    idItem = listViewItem.Items[i].SubItems[0].Text;
                    txtNameItem.Text = listViewItem.Items[i].SubItems[1].Text;;
                    cbbTypeItem.Text = listViewItem.Items[i].SubItems[2].Text;
                    inputPrice.Text = listViewItem.Items[i].SubItems[3].Text;
                    cbbMode.Text = listViewItem.Items[i].SubItems[4].Text;
                    txtNote.Text = listViewItem.Items[i].SubItems[5].Text;                    
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                NomDataContext nc = new NomDataContext();
                var itemList = from item in nc.nom_items
                               where item.id != Convert.ToInt16(idItem)
                               select item;
                    foreach (var item in itemList)
                    {
                        if (txtNameItem.Text.Trim().ToLower().Equals(item.name.Trim().ToLower()))
                        {
                            flag = true;
                        }
                    }
                    if (flag == true)
                    {
                        MessageBox.Show(languageManager.ALREADY_ITEM_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        nom_item item = nc.nom_items.Single(i => i.id == Convert.ToInt16(idItem));
                        item.name = txtNameItem.Text;
                        item.item_category_id = Convert.ToInt16(cbbTypeItem.SelectedValue);
                        item.price = inputPrice.Value;
                        item.specification_id = Convert.ToInt16(cbbMode.SelectedValue);
                        item.description = txtNote.Text;

                        nc.SubmitChanges();

                        btnRenew_Click(sender, e);
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }        
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                NomDataContext nc = new NomDataContext();
                nom_item item = nc.nom_items.Single(i => i.id == Convert.ToInt16(idItem));
                nc.nom_items.DeleteOnSubmit(item);
                nc.SubmitChanges();
                
                btnRenew_Click(sender, e);
            }
            catch (Exception)
            {
                MessageBox.Show(languageManager.DELETE_ITEM_ERROR, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void inputPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (inputPrice.Text == "")
                {
                    MessageBox.Show(languageManager.INSERT_ITEM_ERROR, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtNameItem.Focus();
                }
                else
                {
                    btnAccept_Click(this, e);
                    txtNameItem.Focus();
                }
            }
        }
    }
}
